#### license: creative commons cc0 1.0 (public domain) 
#### http://creativecommons.org/publicdomain/zero/1.0/ 
proginf = "vpa 0.1, jun 2018 mn"

function update
    nl 10 chr
    nla nl plus "</a>"
    now "loading repos" print
    repos arrcurl "https://repo.voidlinux.eu/current/" join repos "" split repos "</a>" join repos nla split repos ">" join repos nl split repos nl
    now "vpa.r" open "w"
    forin p repos
        grep instr p ".i686.xbps"
        filt instr p "<"
        iftrue grep
            ifequal filt 0
                now p fprint "vpa.r"
                fig
            fig
        next
    now "vpa.r" close
    fig

function search repos p
    nl 10 chr
    now "searching repos for: " prints colortext 15
    now p prints colortext 7 "" print print
    forin s repos
        grep instr s p
        filt instr s ".xbps.sig"
        iftrue grep
            ifequal filt 0
                now s print
                fig
            fig
        next
    fig

function download repos p
    nl 10 chr
    nlt 9 chr plus nl reverse
    tabbed repos join tabbed nlt split tabbed nl
    now "downloading: " prints colortext 15
    plen p len minus 1
    now p left plen prints colortext 7 "" print print
    forin num "0123456789"
        pnum p plus num 
    forin d tabbed
        grep instr d pnum
        filt instr d ".xbps.sig"
        iftrue grep
            ifequal filt 0
                pname split d tab join d "" ltrim
                url "https://repo.voidlinux.eu/current/" plus pname
                try
                    now "downloading: " plus url plus " " plus url plus ".sig" print
                    now "rm " plus pname plus " " plus pname plus ".sig" plus " 2> /dev/null" shell
                    now "wget " plus url shell
                    now "wget " plus url plus ".sig" shell
                except
                    pass
                resume 
                fig
            fig
        next
        next
    fig

now "/opt/vpa" chdir

searchfor ""
downloadname ""

while

    findp command instr findp "search" 
    iftrue findp
        try
            p command mid findp 2 arr mid 2 1
            plen p len
            ifequal plen 0
                now divby 0
                fig
        except
            now "to search, enter part of a package name after the search command: " prints 
            now colortext 15 "vpa search python" prints colortext 7 "" print system
            resume
        iftrue p
            searchfor p
            fig
        break
        fig

    findp command instr findp "download"
    iftrue findp
        try
            p command mid findp 2 arr mid 2 1
            plen p len
            ifequal plen 0
                now divby 0
                fig
        except
            now "to download a package, enter the package name after the search command: " prints 
            now colortext 15 "vpa download leafpad" prints colortext 7 "" print system
            resume
        iftrue p
            tab 9 chr
            downloadname tab plus p plus "-"
            fig
        break
        fig
    
    upd command instr upd "update" 
    iftrue upd
        now update
        break
        fig
    
    break

    wend

try
    repos arropen "vpa.r"
except
    repos "" arr
    resume

count repos len divby 2 int
ifmore count 1
    now count prints " packages" print
else
    now "no packages; recommend running " prints colortext 15 "vpa update" prints colortext 7 "" print system
    fig

iftrue searchfor
    now search repos searchfor
    fig

iftrue downloadname
    now download repos downloadname
    fig
